package backed.controller;

import backed.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/file")
@CrossOrigin
@Tag(name = "文件管理", description = "文件上传、删除相关接口")
public class FileUploadController {
    
    @Autowired
    private FileUploadService fileUploadService;
    
    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "上传文件到腾讯云COS，支持图片、文档等多种格式，最大10MB")
    @io.swagger.v3.oas.annotations.responses.ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "上传成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "文件大小超限或格式错误"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器错误")
    })
    public ResponseEntity<Map<String, Object>> uploadFile(
            @Parameter(description = "上传的文件", required = true)
            @RequestParam("file") MultipartFile file,
            @Parameter(description = "用户ID", required = true)
            @RequestParam("userId") Long userId) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 文件大小限制：10MB
            if (file.getSize() > 10 * 1024 * 1024) {
                result.put("success", false);
                result.put("message", "文件大小不能超过10MB");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
            }
            
            // 上传文件，传入用户ID
            String fileUrl = fileUploadService.uploadFile(file, userId);
            
            result.put("success", true);
            result.put("message", "上传成功");
            result.put("data", Map.of(
                "url", fileUrl,
                "fileName", file.getOriginalFilename(),
                "fileSize", file.getSize()
            ));
            
            return ResponseEntity.ok(result);
            
        } catch (IllegalArgumentException e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "上传失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
    
    @DeleteMapping("/delete")
    @Operation(summary = "删除文件", description = "从腾讯云COS删除文件")
    @io.swagger.v3.oas.annotations.responses.ApiResponses({
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功"),
        @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "删除失败")
    })
    public ResponseEntity<Map<String, Object>> deleteFile(
            @Parameter(description = "文件URL", required = true)
            @RequestParam("fileUrl") String fileUrl) {
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            fileUploadService.deleteFile(fileUrl);
            
            result.put("success", true);
            result.put("message", "删除成功");
            return ResponseEntity.ok(result);
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}
